package com.youlu.campus.service.visitor.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.UserAddressInfo;
import com.youlu.campus.service.visitor.UserAddressInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.Date;

@Service
@Slf4j
public class UserAddressInfoServiceImpl implements UserAddressInfoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public UserAddressInfo findByUserId(@NotNull String userId) {
        log.info(":>>> 查找用户地址信息:{}", userId);
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query, UserAddressInfo.class);
    }

    @Override
    public void update(@NotNull UserAddressInfo req) {
        if (StringUtils.isBlank(req.getId()) && StringUtils.isBlank(req.getUserId())) {
            log.error(":>>> 修改用户地址参数错误");
            throw new BusinessException("修改用户地址参数错误");
        }
        Query query = new Query();
        if (StringUtils.isNotBlank(req.getId())) {
            query.addCriteria(Criteria.where("id").is(req.getId()));
        } else {
            return;
        }
        if (StringUtils.isNotBlank(req.getUserId())) {
            query.addCriteria(Criteria.where("userId").is(req.getUserId()));
        }
        Update update = new Update();
        boolean toUpdate = false;
        if (StringUtils.isNotBlank(req.getProvince())) {
            update.set("province", req.getProvince());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(req.getCity())) {
            update.set("city", req.getCity());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(req.getArea())) {
            update.set("area", req.getArea());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(req.getFullAddress())) {
            update.set("fullAddress", req.getFullAddress());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(req.getRecieveName())) {
            update.set("recieveName", req.getRecieveName());
            update.set("name", req.getRecieveName());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(req.getRecieveMobile())) {
            update.set("recieveMobile", req.getRecieveMobile());
            update.set("mobile", req.getRecieveMobile());
            toUpdate = true;
        }
        if (toUpdate) {
            update.set("updatedTime", new Date());
            UpdateResult updateResult = mongoTemplate.updateMulti(query, update, UserAddressInfo.class);
            log.info(":>>> 更新用户地址数量:{},{}", JSON.toJSON(req), updateResult.getModifiedCount());
        }
    }

    @Override
    public UserAddressInfo create(UserAddressInfo req) {
        req.setId(null);
        req.setCreatedTime(new Date());
        req.setName(req.getRecieveName());
        req.setMobile(req.getRecieveMobile());
        return mongoTemplate.insert(req);
    }

    @Override
    public boolean checkUserAddress(UserAddressInfo req) {
        log.info(":>>> 开始检查用户地址:{}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getProvince()) || StringUtils.isBlank(req.getCity()) || StringUtils.isBlank(req.getArea()) ||
                StringUtils.isBlank(req.getFullAddress()) || StringUtils.isBlank(req.getRecieveName()) || StringUtils.isBlank(req.getRecieveMobile())) {
            return false;
        }
        return true;
    }
}
